package com.demo.auth.service;

import com.alibaba.fastjson.JSON;
import com.demo.auth.dao.domain.AuthCodeUserInterface;
import com.demo.common.constants.AuthRedisKey;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;

@Service
public class TokenStoreService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private final Duration timeout = Duration.ofMinutes(60);

    public void setAdminToken(String userId, OAuth2AccessToken accessToken) {
        String key = AuthRedisKey.usernamePasswordTokenKey(userId);
        stringRedisTemplate.opsForValue().set(key, accessToken.getValue(), timeout);
    }

    public void setAuthCodeToken(String userId, OAuth2AccessToken accessToken) {
        String key = AuthRedisKey.authorizationCodeTokenKey(userId);
        stringRedisTemplate.opsForValue().set(key, accessToken.getValue(), timeout);
    }

    public void setAuthUserInterfaces(String userId, List<AuthCodeUserInterface> list) {
        String key = AuthRedisKey.authorizationCodeUserInterfaceKey(userId);
        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(list), timeout);
    }
}
